<template>
  <div>
    <h1>个人信息</h1>
    <el-table :data="userOnelist" border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="username" label="用户名"></el-table-column>
      <el-table-column prop="name" label="真实姓名"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="gender" label="性别"></el-table-column>
      <el-table-column prop="birthday" label="出生日期"></el-table-column>
      <el-table-column prop="school" label="毕业学校"></el-table-column>
      <el-table-column prop="phone" label="联系电话"></el-table-column>
      <el-table-column prop="email" label="电子邮箱"></el-table-column>
      <el-table-column prop="specialty" label="所学专业"></el-table-column>
      <el-table-column prop="education" label="最高学历"></el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click="showEditDialog(scope.row.username)" type="text" size="small">编辑</el-button>
          <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='算了'
            icon="el-icon-info"
            icon-color="red"
            title="确定要删除这条内容吗？"
            @confirm=deleteInfo(scope.row.id)
          >
            <el-button slot="reference" type="text" size="small">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--编辑-->
    <el-dialog title="编辑" :close-on-click-modal="false" :visible.sync="editDialog">
      <el-form status-icon :model="editForm" ref="editFormRef"
               label-width="70px">
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="editForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input show-password type="password" v-model="editForm.password"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="name">
          <el-input type="text" v-model="editForm.name"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input type="number" v-model="editForm.age"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-input type="text" v-model="editForm.gender"></el-input>
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-input type="date" v-model="editForm.birthday"></el-input>
        </el-form-item>
        <el-form-item label="毕业学校" prop="school">
          <el-input type="text" v-model="editForm.school"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input type="tel" v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input type="email" v-model="editForm.email"></el-input>
        </el-form-item>
        <el-form-item label="所学专业" prop="specialty">
          <el-input type="text" v-model="editForm.specialty"></el-input>
        </el-form-item>
        <el-form-item label="最高学历" prop="education">
          <el-input type="text" v-model="editForm.education"></el-input>
        </el-form-item>
      </el-form>
      <div class="register-button" slot="footer">
        <el-button @click="editDialog=false">取 消</el-button>
        <el-button type="primary" @click="editInfo('editFormRef')">提交修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Qs from "qs";

  export default {
    name: "userOneInfo",
    data() {
      return {
        //获取用户列表的数据
        userOnelist: [],
        username: this.$store.state.username,
        //用户编辑信息对象
        editForm:{},
        //用户编辑对话框开关
        editDialog:false,
      }
    },
    //初始化方法
    created() {
      this.getUserOnelist()
    },

    methods: {
      async getUserOnelist() {
        let _this = this;
        console.log(_this.username);
        await this.$axios.post('/api/user/likeOneInfo', Qs.stringify({
          username: _this.username
        })).then((res) => {
          console.log(res);
          if (res.status !== 200) {
            return _this.$message.error("用户数据获取失败")
          }
          _this.userOnelist = res.data.data
        })
      },
//展示编辑之前的信息
      async showEditDialog(userName){
        console.log(userName);
        const {data:res} = await this.$axios.post("/api/user/getOneInfo", Qs.stringify({username: userName}))
        console.log(res)
        if(res.code!==200){
          this.$message.error("查询用户信息失败")
        }
        this.editForm=res.data
        console.log(res.data)
        this.editDialog=true
      },

      //确认修改方法
      async editInfo(formName){
        this.$refs[formName].validate(async valid => {
          if (!valid) return this.$message.error("参数错误");
          let _this = this;
          const {data: res} = await _this.$axios.put("/api/user/editOneInfo/", Qs.stringify({
            id: _this.editForm.id,
            username: _this.editForm.username,
            password: _this.editForm.password,
            name: _this.editForm.name,
            age: _this.editForm.age,
            gender: _this.editForm.gender,
            birthday: _this.editForm.birthday,
            school: _this.editForm.school,
            phone: _this.editForm.phone,
            email: _this.editForm.email,
            specialty: _this.editForm.specialty,
            education: _this.editForm.education,
          }))
          console.log(res)
          if (res.code !== 200) {
            return _this.$message.error("数据更新失败!");
          }
          _this.editDialog = false;
          _this.getUserOnelist();
          _this.$message.success("数据更新成功!")
        })
      },
      //删除一条信息
      async deleteInfo(id) {
        let _this = this;
        console.log(id);
        const {data:res} = await _this.$axios.post("/api/user/deleteInfo",Qs.stringify({id:id}))
        console.log(res)
        if(res.code!==200){
          return _this.$message.error(res.message);
        }
        this.getUserOnelist();
        return _this.$message.success(res.message);
      },

    }
  }
</script>

<style scoped>

</style>
